package com.elin4it.blog.controller;

import com.elin4it.blog.pojo.Article;
import com.elin4it.blog.pojo.ArticleCustom;
import com.elin4it.blog.service.ArticleService;
import com.elin4it.blog.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by �� on 2015/7/14.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ConfigService configService;
    @Autowired
    private ArticleService articleService;

    private final String flag = "index";

    /**
     * 把一些公共的信息放入modelmap中
     *
     * @param map
     * @throws Exception
     */
    @ModelAttribute
    public void commonMessage(ModelMap map) throws Exception {
        //得到标题和副标题
        String title = configService.findTitle();
        String subTitle = configService.findSubTitle();

        //把标题和副标题放入到model中
        map.put("title", title);
        map.put("subtitle", subTitle);
        //设置页面标识
        map.put("flag",flag);

    }

    /**
     * 用户请求主页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping({"/","/index"})
    public String index(ModelMap modelMap) throws Exception {

        List<ArticleCustom> list = articleService.findArticleTopTenByTime();


        modelMap.put("articles",list);


        return "index/index";
    }
}
